home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 12 / BBS in a box XII-2.iso / Files II / Prog / D-G / Extension Shell 1.3.sit / Extension Shell 1.3 ƒ / Extension Shell 1.3 (Source) / UninstallCode.c / UninstallCode.c
Encoding:
Text File  |  1994-04-06  |  7.6 KB  |  331 lines  |  [TEXT/R*ch]

  1. /*    NAME:
  2.         UninstallCode.c
  3.  
  4.     WRITTEN BY:
  5.         Dair Grant
  6.                 
  7.     DESCRIPTION:
  8.         Routines for uninstalling the various code resources Extension Shell supports.
  9.  
  10.     NOTES:
  11.         •    Not everything that can be installed can be uninstalled.
  12.  
  13.     ___________________________________________________________________________
  14.  
  15.     VERSION HISTORY:
  16.         (Jan 1994, dg)
  17.             •    Added low-memory filter support.
  18.  
  19.         (Jan 1994, dg)
  20.             •    First publicly distributed version.
  21.  
  22.         (Mar 1994, dg)
  23.             •    Added support for code blocks.
  24.             
  25.             
  26.     ___________________________________________________________________________
  27. */
  28. //=============================================================================
  29. //        Include files                                                                     
  30. //-----------------------------------------------------------------------------
  31. #include <GestaltEqu.h>
  32. #include "ParamBlock.h"
  33. #include "UninstallCode.h"
  34. #include "ExtensionShell.h"
  35. #include "AddrsTable.h"
  36.  
  37.  
  38.  
  39.  
  40.  
  41. //=============================================================================
  42. //        Private function prototypes                                                                     
  43. //-----------------------------------------------------------------------------
  44. void    UninstallTrapPatch(short i);
  45. void    UninstallGestaltSelector(short i);
  46. void    UninstallShutdownTask(short i);
  47. void    UninstallVBLTask(short i);
  48. void    UninstallLowMemFilter(short i);
  49. void    UninstallCodeBlock(short i);
  50. void    UninstallTimeManagerTask(short i);
  51.  
  52.  
  53.  
  54.  
  55.  
  56. //=============================================================================
  57. //        Global variables                                                                 
  58. //-----------------------------------------------------------------------------
  59. extern ESParamBlock        gTheParamBlock;
  60. extern AddressTable        *gTheAddressTable;
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. //=============================================================================
  72. //        UninstallCode : Uninstall a code resource depending on its type.                                                                 
  73. //-----------------------------------------------------------------------------
  74. //        Note :    Not all types can be removed successfully. Right now, we still
  75. //                don't know of a clean way to remove a Gestalt Selector.
  76. //-----------------------------------------------------------------------------
  77. void UninstallCode(short i)
  78. {
  79.  
  80.  
  81.  
  82.  
  83.     // Case out on the type of the code, and call the uninstall routine
  84.     switch(gTheParamBlock.theCodeResources[i].codeType) {
  85.         case kTrapPatchType:
  86.              UninstallTrapPatch(i);
  87.              break;
  88.              
  89.         case kGestaltSelectorType:
  90.              UninstallGestaltSelector(i);
  91.              break;
  92.              
  93.         case kShutdownTaskType:
  94.              UninstallShutdownTask(i);
  95.              break;
  96.              
  97.         case kVBLTaskType:
  98.              UninstallVBLTask(i);
  99.              break;
  100.         
  101.         case kLowMemFilterType:
  102.              UninstallLowMemFilter(i);
  103.              break;
  104.         
  105.         case kCodeBlockType:
  106.              UninstallCodeBlock(i);
  107.              break;
  108.         
  109.         case kTimeManagerTaskType:
  110.              UninstallTimeManagerTask(i);
  111.              break;
  112.              
  113.         default:
  114.              ;
  115.     }
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. //=============================================================================
  127. //        UninstallTrapPatch : Uninstall a trap patch.                                                         
  128. //-----------------------------------------------------------------------------
  129. //        Note :    We check the value currently held in the dispatch table. If
  130. //                it's still the same as our address, then it's safe to remove
  131. //                our routine. We do this just by replacing the dispatch table
  132. //                entry with the old value we originally replaced.
  133. //-----------------------------------------------------------------------------
  134. void UninstallTrapPatch(short i)
  135. {    int            trapNum;
  136.     ProcPtr        currentCode;
  137.     ProcPtr        oldCodeAddress;
  138.     TrapType    tType;
  139.     ProcPtr        currentDispatch;
  140.     Handle        theHnd;
  141.     
  142.  
  143.     
  144.  
  145.      // Copy the relevent details into local variables for speed
  146.      trapNum            = gTheParamBlock.theCodeResources[i].theCodeThing.theTrapPatch.trapNum;
  147.      currentCode        = (ProcPtr) gTheParamBlock.theCodeResources[i].theAddress;
  148.      oldCodeAddress    = gTheAddressTable->theTable[i];
  149.      
  150.      
  151.      
  152.      // Get the type of the trap
  153.     tType = GetTrapType(trapNum);
  154.     
  155.     
  156.     
  157.     // Compare the value in the dispatch table with our own code. If they match,
  158.     // then it's safe to remove the patch. Otherwise we're stuck.
  159.     currentDispatch = (ProcPtr) StripAddress((Ptr) NGetTrapAddress(trapNum, tType));
  160.     if (currentDispatch == currentCode)
  161.         {
  162.         // Overwrite the dispatch table entry with the old address.
  163.         NSetTrapAddress((long) StripAddress(oldCodeAddress), trapNum, tType);
  164.    
  165.    
  166.         
  167.         // Recover a handle to our code, and throw it away
  168.         theHnd = RecoverHandle(currentCode);
  169.         HUnlock(theHnd);
  170.         DisposHandle(theHnd);
  171.         }
  172. }
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183. //=============================================================================
  184. //        UninstallGestaltSelector : Uninstall a Gestalt Selector.                                                         
  185. //-----------------------------------------------------------------------------
  186. //        Note :    I don't know a way of removing a Gestalt Selector at present.
  187. //                We just have to leave them floating around for now. :-(
  188. //-----------------------------------------------------------------------------
  189. void UninstallGestaltSelector(short i)
  190. {
  191. }
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. //=============================================================================
  203. //        UninstallShutdownTask : Uninstall a Shutdown Task.                                                         
  204. //-----------------------------------------------------------------------------
  205. void UninstallShutdownTask(short i)
  206. {
  207.  
  208.  
  209.     // Remove the shutdown task
  210.     ShutDwnRemove((ShutDwnProcPtr) gTheParamBlock.theCodeResources[i].theAddress);
  211. }
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222. //=============================================================================
  223. //        UninstallVBLTask : Uninstall a VBL task.                                                         
  224. //-----------------------------------------------------------------------------
  225. void UninstallVBLTask(short i)
  226. {
  227.  
  228.  
  229.     // Remove the VBL task
  230.     VRemove((QElemPtr) gTheParamBlock.theCodeResources[i].theAddress);
  231. }
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242. //=============================================================================
  243. //        UninstallLowMemFilter : Remove a low-memory filter.                                                         
  244. //-----------------------------------------------------------------------------
  245. void UninstallLowMemFilter(short i)
  246. {    Handle        theHnd;
  247.     ProcPtr        theEntryPoint, currentHead, oldHead, ourCode;
  248.  
  249.  
  250.     
  251.     
  252.     // Get the address of the filter chain
  253.     theEntryPoint = (ProcPtr) gTheParamBlock.theCodeResources[i].theCodeThing.theLowMemFilter.theEntryPoint;
  254.     
  255.     
  256.     
  257.     // Find out where everything else is
  258.     currentHead    = (ProcPtr) *((ProcPtr *) theEntryPoint);
  259.     oldHead        = gTheAddressTable->theTable[i];
  260.     ourCode        = gTheParamBlock.theCodeResources[i].theAddress;
  261.     
  262.     
  263.     
  264.     // Compare the value at the head of the chain with our own code. If
  265.     // they match, then we can remove our filter. Otherwise we're stuck.
  266.     if (currentHead == ourCode)
  267.         {
  268.         // Overwrite the entry at the head of the chain with the original
  269.         *((ProcPtr *) theEntryPoint) = oldHead;
  270.         
  271.         
  272.         
  273.         // Recover a handle to our code, and throw it away
  274.         theHnd = RecoverHandle(ourCode);
  275.         HUnlock(theHnd);
  276.         DisposHandle(theHnd);
  277.         }
  278. }
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289. //=============================================================================
  290. //        UninstallCodeBlock : Remove a block of code.                                                         
  291. //-----------------------------------------------------------------------------
  292. void UninstallCodeBlock(short i)
  293. {    ProcPtr        theCode;
  294.     Handle        theHnd;
  295.     
  296.  
  297.     
  298.  
  299.      // Get the pointer to the code block
  300.      theCode = (ProcPtr) gTheParamBlock.theCodeResources[i].theAddress;
  301.  
  302.      
  303.      
  304.      // Recover a handle and dispose of it
  305.      theHnd = RecoverHandle(theCode);
  306.     HUnlock(theHnd);
  307.     DisposHandle(theHnd);
  308. }
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319. //=============================================================================
  320. //        UninstallTimeManagerTask : Remove a Time Manager task.                                                         
  321. //-----------------------------------------------------------------------------
  322. void UninstallTimeManagerTask(short i)
  323. {
  324.  
  325.  
  326.  
  327.     // Remove the Time Manager task
  328.     RmvTime((QElemPtr) gTheParamBlock.theCodeResources[i].theAddress);
  329. }
  330.